//
//  CMDBAssociateCollectionViewCell.swift
//  CMDB
//
//  Created by 王磊 on 09/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBAssociateCollectionViewCell: UICollectionViewCell {
    
    internal var indexPath: NSIndexPath?
    
    private var imageView: UIImageView = UIImageView()
    
    private var cover: UIImageView = UIImageView()
    
    private var selectIcon: UIButton = UIButton(type: .Custom)
    
    private var textLabel: UILabel = UILabel()
    
    private var label: UILabel = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addOwnSubviews()
        
        configOwnSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension CMDBAssociateCollectionViewCell {
    
    func updateData(data: AnyObject?) {
        
        let works = data as! CMDBWorksBean
        
        imageView.loadPicImage(works.poster)
        
        textLabel.text = works.name
    }
    
}

extension CMDBAssociateCollectionViewCell {
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        contentView.addSubview(imageView)
        
        contentView.addSubview(textLabel)
        
        contentView.addSubview(label)
        
    }
    
    override func configOwnSubviews() {
        super.configOwnSubviews()
        textLabel.font = CMDB_13_FONT
        
        textLabel.textAlignment = .Left
        
        textLabel.lineBreakMode = .ByTruncatingTail
        
        textLabel.textColor = word87Color
        
        contentView.addSubview(cover)
        
        cover.hidden = true
        
        cover.backgroundColor = word87Color
        
        cover.alpha = 0.3
        
        contentView.addSubview(selectIcon)
        
        selectIcon.setImage(UIImage(named: "reg-associateworks-select-0")!, forState: .Normal)
        
        selectIcon.setImage(UIImage(named: "reg-associateworks-select-1")!, forState: .Selected)
        
        selectIcon.selected = false
    }
}
extension CMDBAssociateCollectionViewCell {
    
    internal func selectWorks(flag: Bool) {
        
        cover.hidden = !flag
        
        selectIcon.selected = flag
    }
}

extension CMDBAssociateCollectionViewCell {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageView.frame = CGRectMake(0, 0, width, width * 4 / 3)
        
        cover.frame = imageView.frame
        
        textLabel.frame = CGRectMake(0, width * 4 / 3, width, Associate_Item_Label_Height)
        
        label.frame = CGRectMake(0, width * 4 / 3 + Associate_Item_Label_Height, width, Associate_Item_Title_Height)
        selectIcon.snp_makeConstraints { (make) in
            
            make.right.equalTo(-10 * CMDB_Rate)
            
            make.bottom.equalTo(-(Associate_Item_Label_Height + Associate_Item_Title_Height + 10 * CMDB_Rate))
        }
    }
}
